<template>
	<view>
		<diy ref="diy" v-if="isDiy && loading" @showCart="showCart"></diy>
		<!-- <visualization ref='vis' v-else-if="!isDiy && loading"></visualization> -->
		<productWindow ref="productWindow" safaButtom :authenticate_type="authenticate_type" @getCount="getCount"
			@isOpen="isOpenHandle"></productWindow>
	</view>

</template>

<script>
	import diy from './diy'
	// import visualization from './visualization'
	import Cache from '@/utils/cache';
	import productWindow from '@/components/productWindowOne';
	import {
		getCartCounts
	} from '@/api/order.js';
	import {
		getUserInfo
	} from "@/api/user.js";
	import {
		getShare,
		getVersion
	} from "@/api/public.js";
	import {
		mapGetters
	} from "vuex";
	let app = getApp();
	export default {
		computed: mapGetters(['isLogin']),
		components: {
			diy,
			// visualization,
			productWindow
		},
		watch: {
			isLogin: {
				handler: function(newV, oldV) {
					if (newV == true) {
						this.getUserInfo()
					}
				},
				deep: true,
			}
		},
		data() {
			return {
				isDiy: uni.getStorageSync('is_diy'),
				shareInfo: {},
				loading: false,
				authenticate_type: 0
			}
		},

		onLoad() {
			uni.hideTabBar()
			// uni.$on('is_diy', (data) => {
			// 	this.isDiy = data
			// })
			this.setOpenShare();
			if (this.isLogin) {
				this.getUserInfo()
			}
		},
		onShow() {
			console.log(this.isDiy, this.loading, "skfjksdjfk");
			this.getVersion(0);
		},
		onReady() {

		},
		onHide() {
			this.$refs.diy.clearTimer()

		},
		onUnload() {
			this.$refs.diy.clearTimer()
		},
		methods: {
			// 判断是否已认证，展示不同的工费
			getUserInfo: function() {
				let that = this;
				getUserInfo().then(res => {
					const data = res.data
					this.authenticate_type = data.authenticate_type
					this.$store.commit('UPDATE_USERINFO', data)
				});
			},
			isOpenHandle(val) {
				// if (val) {
				// 	this.$refs.diy.clearTimer()
				// } else {
				// 	this.$refs.diy.homepage_goldHandle()
				// }
				console.log(val, "是否打开");
			},
			getCount() {
				this.getCartNum()
			},
			getCartNum: function() {
				let that = this;
				getCartCounts().then(res => {
					this.$store.commit('indexData/setCartNum', res.data.count > 99 ? '..' : res.data.count)
					if (res.data.count > 0) {
						wx.setTabBarBadge({
							index: 2,
							text: res.data.count + ''
						})
					} else {
						wx.hideTabBarRedDot({
							index: 2
						})
					}

				});
			},
			showCart(item) {
				this.$refs.productWindow.setStoreInfoFun(item.id, 'buyMaterials')
			},
			getVersion(name) {
				uni.$emit('uploadFooter')
				getVersion(name).then(res => {
					this.version = res.data.version
					this.isDiy = res.data.is_diy
					this.loading = true
					this.$nextTick(()=>{
						this.$refs.diy.homepage_goldHandle()
						if (this.isLogin) {
							this.$refs.diy.getUserInfo()
						}
					})
					
					uni.setStorageSync('is_diy', res.data.is_diy)
					if (!uni.getStorageSync('DIY_VERSION') || res.data.version != uni.getStorageSync(
							'DIY_VERSION')) {
						if (uni.getStorageSync('DIY_VERSION')) {
							uni.setStorageSync('DIY_VERSION', res.data.version)
							if (this.isDiy) {
								this.$refs.diy.reconnect()
							} else {
								this.$refs.vis.reconnect()
							}
						}
						uni.setStorageSync('DIY_VERSION', res.data.version)
					} else {}
				}).catch(err => {
					// #ifdef APP-PLUS
					setTimeout(e => {
						this.getVersion(0);
					}, 1500)
					// #endif
					// #ifndef APP-PLUS
					this.$util.Tips({
						title: err
					});
					// #endif
				})
			},
			// 微信分享；
			setOpenShare: function() {
				let that = this;
				getShare().then((res) => {
					let data = res.data;
					this.shareInfo = data;
					// #ifdef H5
					let url = location.href;
					if (this.$store.state.app.uid) {
						url =
							url.indexOf("?") === -1 ?
							url + "?spread=" + this.$store.state.app.uid :
							url + "&spread=" + this.$store.state.app.uid;
					}
					if (that.$wechat.isWeixin()) {
						let configAppMessage = {
							desc: data.synopsis,
							title: data.title,
							link: url,
							imgUrl: data.img,
						};
						that.$wechat.wechatEvevt(
							["updateAppMessageShareData", "updateTimelineShareData"],
							configAppMessage
						);
					}
					// #endif
				});
			},
		},
		onReachBottom: function() {
			if (this.isDiy) {
				this.$refs.diy.onsollBotton()
			}
		},
		// 滚动监听
		onPageScroll(e) {
			// 传入scrollTop值并触发所有easy-loadimage组件下的滚动监听事件
			uni.$emit('onPageScrollHandle',e);
		},
		// #ifdef MP
		//发送给朋友
		onShareAppMessage(res) {
			// 此处的distSource为分享者的部分信息，需要传递给其他人
			let that = this;
			return {
				title: this.shareInfo.title,
				path: "/pages/index/index?spid=" + this.$store.state.app.uid || 0,
				imageUrl: this.shareInfo.img,
			};
		},
		//分享到朋友圈
		onShareTimeline() {
			return {
				title: this.shareInfo.title,
				query: {
					spid: this.$store.state.app.uid || 0
				},
				imageUrl: this.shareInfo.img,
			};
		},
		// #endif
	}
</script>

<style>

</style>